﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using Portal.Utility;
using Portal.Web.Utility;
using Portal.Web.Model;
using Portal.Exam.Bussiness;
using Portal.Exam.Model;
using Portal.Core;

namespace Portal.Exam.Render
{
    public class UpdateExamTemplate
    {
        public static AjaxOut Draw(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                string IconRoot = WebConfig.GetButtonIconRoot(OSiteParam);

                string ExamTemplateId = System.Web.HttpContext.Current.Request["ExamTemplateId"];
                ExamTemplateCls ExamTemplateCls = new ExamTemplateBll().CreateModel(SiteId, ExamTemplateId);

                string AccountType = "";
                try
                {
                    AccountType = System.Web.HttpContext.Current.Request["AccountType"];
                }
                catch { }

                string Checked = "";
                if (ExamTemplateCls.ExamTemplateType.ToLower().Equals("freetext"))
                {
                    Checked = "Checked";
                }
                if (ExamTemplateCls == null)
                {
                    OAjaxOut.HtmlContent = "Không tìm thấy dữ liệu truy cập không đúng";
                    return OAjaxOut;
                }
                string BackUrl = "ExamTemplate.aspx";
                string SelectActiveExam = "<select id=drpSelectActiveExam>\r\n" +
                                          " <option>Không sử dụng</option>\r\n" +
                                          " <option>Sử dụng</option>\r\n" +
                                          "</select>";
                if (ExamTemplateCls.ActiveExam == 1)
                {
                    SelectActiveExam = "<select id=drpSelectActiveExam>\r\n" +
                                       " <option>Không sử dụng</option>\r\n" +
                                       " <option selected>Sử dụng</option>\r\n" +
                                       "</select>";
                }
                string RetHtml =

                #region javascript
 "<script language=\"javascript\">\r\n" +
                    "   function ResetSearch()\r\n" +
                    "   {\r\n" +
                    "   }\r\n" +

                    "   function ClientSaveUpdateExamPersonal(WhoIsExamId)\r\n" +
                    "    {\r\n" +
                    "        SiteId='" + SiteId + "';\r\n" +
                    "        IdCard=document.getElementById(\"txtIdCard\").value;\r\n" +
                    "        FullName=document.getElementById(\"txtFullName\").value;\r\n" +
                    "        Address=document.getElementById(\"txtAddress\").value;\r\n" +
                    "        Tel=document.getElementById(\"txtTel\").value;\r\n" +
                    "        Sex=document.getElementById(\"drpSex\").options[document.getElementById(\"drpSex\").selectedIndex].value;\r\n" +
                    "        RetValue = Portal.Exam.Render.UpdateExamPersonal.ServerSideUpdateExamPersonal(SiteId, WhoIsExamId,FullName, Address, Tel, Sex,IdCard).value;\r\n" +
                    "        if(RetValue.indexOf(\"ERROR\")!=-1)\r\n" +
                    "        {\r\n" +
                    "            alert(RetValue);\r\n" +
                    "            return;\r\n" +
                    "        }\r\n" +
                    "        window.open('" + BackUrl + "','_self');\r\n" +
                    "    }\r\n" +

                    " function DetectMonthi() {\r\n" +
                    "    SiteId='" + SiteId + "';\r\n" +
                    "    GroupQuestionId = document.getElementById(\"drpSelectExamCollection\").options[document.getElementById(\"drpSelectExamCollection\").selectedIndex].value;\r\n" +
                    "    retValue = Portal.Exam.Render.UpdateExamTemplate.ReadingMonthi(SiteId, GroupQuestionId).value;\r\n" +
                    "    if (retValue.indexOf(\"ERROR\") != -1) {\r\n" +
                    "         alert(retValue);\r\n" +
                    "         return;\r\n" +
                    "    }\r\n" +
                    "    document.getElementById(\"divMonthi\").innerHTML = retValue;\r\n" +
                    "  }\r\n" +

                    " function ClientDetectSubExamCollection() {\r\n" +
                    "    SiteId='" + SiteId + "';\r\n" +
                    "    ExamCollectionId = document.getElementById(\"drpSelectExamCollection\").value;\r\n" +
                    "    retValue = Portal.Exam.Render.UpdateExamTemplate.GetSubExamCollection(SiteId, ExamCollectionId).value;\r\n" +
                    "    if (retValue.indexOf(\"ERROR\") != -1) {\r\n" +
                    "         alert(retValue);\r\n" +
                    "         return;\r\n" +
                    "    }\r\n" +
                    "    document.getElementById(\"divSubExamCollection\").innerHTML = retValue;\r\n" +
                    "  }\r\n" +


                    " function ClientSaveUpdateExamTemplate(ExamTemplateId)\r\n" +
                    " {\r\n" +
                    "    SiteId='" + SiteId + "';\r\n" +
                    "     ExamTemplateName=document.getElementById(\"txtExamTemplateName\").value;\r\n" +
                    "     ExamTime=document.getElementById(\"txtExamTime\").value;\r\n" +
                    "     MaxQuestion=document.getElementById(\"txtMaxQuestion\").value;\r\n" +
                    "     PassPoint=document.getElementById(\"txtPassPoint\").value;\r\n" +
                    "     IndexOrder=document.getElementById(\"txtIndexOrder\").value;\r\n" +
                    "     BeforeExamGuidle=document.getElementById(\"txtBeforeExamGuidle\").value;\r\n" +
                    "     ActiveExam=document.getElementById(\"drpSelectActiveExam\").selectedIndex;\r\n" +
                    "     IsFreeText= document.getElementById(\"chkIsFreeText\").checked; \r\n" +
                    "     RetValue = Portal.Exam.Render.UpdateExamTemplate.ServerSideUpdateExamTemplate(SiteId, ExamTemplateId,ExamTemplateName,ExamTime,MaxQuestion,PassPoint,IndexOrder,BeforeExamGuidle, ActiveExam,IsFreeText).value;\r\n" +
                    "     if(RetValue.indexOf(\"ERROR\")!=-1)\r\n" +
                    "     {\r\n" +
                    "         alert(RetValue);\r\n" +
                    "         return;\r\n" +
                    "     }\r\n" +
                    "     alert(RetValue);\r\n" +
                    " }\r\n" +


                    " function Add2TemplateByAccountType(ExamTemplateId)\r\n" +
                    "{\r\n" +
                    "    SiteId='" + SiteId + "';\r\n" +
                    "    if(document.getElementById(\"txtSelectedGroupQuestionId\") == null)\r\n" +
                    "    {\r\n" +
                    "       alert('Chưa xác định nhóm câu hỏi!');\r\n" +
                    "       return;\r\n" +
                    "    }\r\n" +
                    //"    ExamCollectionId=document.getElementById(\"drpSelectSubExamCollection\").value;\r\n" +
                    "    GroupQuestionId=document.getElementById(\"txtSelectedGroupQuestionId\").value;\r\n" +
                    "    QuestionType=document.getElementById(\"drpSelectType\").options[document.getElementById(\"drpSelectType\").selectedIndex].value;\r\n" +
                    "    Question=document.getElementById(\"txtQuestion\").value;\r\n" +
                    "    SelectLevelIndex = document.getElementById(\"drpSelectLevelQuestion\").selectedIndex; \r\n" +
                    "    SelectLevel = document.getElementById(\"drpSelectLevelQuestion\").options[SelectLevelIndex].value; \r\n" +
                    "    RetValue = Portal.Exam.Render.UpdateExamTemplate.ServerSideAddExamTemplateConfigNew(SiteId, ExamTemplateId,GroupQuestionId,QuestionType,Question, SelectLevel).value;\r\n" +
                    "    if(RetValue.indexOf(\"ERROR\")!=-1)\r\n" +
                    "    {\r\n" +
                    "        alert(RetValue);\r\n" +
                    "        return;\r\n" +
                    "    }\r\n" +
                    "    alert(RetValue);\r\n" +
                    "    document.getElementById(\"divConfigItem\").innerHTML=Portal.Exam.Render.UpdateExamTemplate.GetConfigListHtmlNew(SiteId, ExamTemplateId).value;\r\n" +
                    "}\r\n" +

                    " function Add2Template(ExamTemplateId)\r\n" +
                    "{\r\n" +
                    "    SiteId='" + SiteId + "';\r\n" +
                    //"    MonthiId=document.getElementById(\"drpSelectMonthi\").options[document.getElementById(\"drpSelectMonthi\").selectedIndex].value;\r\n"+
                    "    if(document.getElementById(\"drpSelectExamCollection\")<0)\r\n" +
                    "    {\r\n" +
                    "       alert('Chưa xác định nhóm câu hỏi');\r\n" +
                    "       return;\r\n" +
                    "    }\r\n" +
                    "    ExamCollectionId=document.getElementById(\"drpSelectSubExamCollection\").value;\r\n" +
                    "    QuestionType=document.getElementById(\"drpSelectType\").options[document.getElementById(\"drpSelectType\").selectedIndex].value;\r\n" +
                    "    Question=document.getElementById(\"txtQuestion\").value;\r\n" +
                    "    SelectLevelIndex = document.getElementById(\"drpSelectLevelQuestion\").selectedIndex; \r\n" +
                    "    SelectLevel = document.getElementById(\"drpSelectLevelQuestion\").options[SelectLevelIndex].value; \r\n" +
                    "    RetValue = Portal.Exam.Render.UpdateExamTemplate.ServerSideAddExamTemplateConfig(SiteId, ExamTemplateId,ExamCollectionId,QuestionType,Question, SelectLevel).value;\r\n" +
                    "    if(RetValue.indexOf(\"ERROR\")!=-1)\r\n" +
                    "    {\r\n" +
                    "        alert(RetValue);\r\n" +
                    "        return;\r\n" +
                    "    }\r\n" +
                    "    alert(RetValue);\r\n" +
                    "    document.getElementById(\"divConfigItem\").innerHTML=Portal.Exam.Render.UpdateExamTemplate.GetConfigListHtml(SiteId, ExamTemplateId).value;\r\n" +
                    "}\r\n" +


                    " function SaveItemConfig(ExamTemplateConfigId) \r\n" +
                    " { \r\n" +
                    "     SiteId='" + SiteId + "';\r\n" +
                    "     Question=document.getElementById(\"txtQuestion_\"+ExamTemplateConfigId).value; \r\n" +
                    "     retValue=Portal.Exam.Render.UpdateExamTemplate.ServerSideSaveItemConfig(SiteId, ExamTemplateConfigId,Question).value; \r\n" +
                    "     alert(retValue); \r\n" +
                    " } \r\n" +

                    " function DeleteItemConfig(ExamTemplateId,ExamTemplateConfigId) \r\n" +
                    " {\r\n" +
                    "     SiteId='" + SiteId + "';\r\n" +
                    "     Msg=Portal.Exam.Render.UpdateExamTemplate.ServerSideCheckDeleteExamTemplateConfig(SiteId, ExamTemplateConfigId).value;\r\n" +
                    "     if(Msg.indexOf(\"ERROR\")!=-1)\r\n" +
                    "     {\r\n" +
                    "         alert(Msg);\r\n" +
                    "         return;\r\n" +
                    "     }\r\n" +
                    "     Msg=Portal.Exam.Render.UpdateExamTemplate.GetConfirmDeleteMsg(SiteId).value;\r\n" +
                    "     if(confirm(Msg)==false)return;\r\n" +
                    "     RetValue = Portal.Exam.Render.UpdateExamTemplate.ServerSideDeleteExamTemplateConfig(SiteId, ExamTemplateConfigId).value;\r\n" +
                    "     if(RetValue.indexOf(\"ERROR\")!=-1)\r\n" +
                    "     {\r\n" +
                    "         alert(RetValue);\r\n" +
                    "         return;\r\n" +
                    "     }\r\n" +
                    "     alert(RetValue);\r\n" +
                    "     document.getElementById(\"divConfigItem\").innerHTML=Portal.Exam.Render.UpdateExamTemplate.GetConfigListHtml(SiteId, ExamTemplateId).value;\r\n" +
                    " }\r\n" +

                    "</script>\r\n" +

                    //GetPluginJavascript(SiteId) +

                #endregion

 " <div class=DatabaseFunction>\r\n" +
                    "        <ul>\r\n" +
                    "            <li onclick=\"javascript:window.open('TemplateGroupQuestionTreeView.aspx?EId=" + ExamTemplateId + "','_self');\"><img class=image src=\"" + IconRoot + "/selection.png\"><div>Chọn câu hỏi</div></li>\r\n" +
                    "            <li onclick=\"javascript:ClientSaveUpdateExamTemplate('" + ExamTemplateId + "');\"><img class=image src=\"" + IconRoot + "/Save.png\"><div>Ghi lại</div></li>\r\n" +
                    "            <li onclick=\"javascript:window.open('" + BackUrl + "','_self');\"><img class=image src=\"" + IconRoot + "/back.png\"><div>Quay lại</div></li>\r\n" +
                    "        </ul>\r\n" +
                    "  </div>\r\n" +

                    "     <h3>Thông tin chung</h3>" +
                    "     <div class=CssFormSaveUpdate>\r\n" +
                    "     <table>" +
                    "     <tr>" +
                    "         <td class=\"Col1\">Trạng thái:</td>" +
                    "         <td>" + SelectActiveExam + "</td>" +
                    "     </tr>" +
                    "     <tr>" +
                    "         <td class=\"Col1\">Giới thiệu trước khi thi:</td>" +
                    "         <td><textarea id=txtBeforeExamGuidle class=textarea type=textarea>" + ExamTemplateCls.BeforeExamGuidle + "</textarea></td>" +
                    "     </tr>" +
                    "          <tr>" +
                    "              <td class=\"Col1\">Đề thi:</td>" +
                    "              <td><input class=textbox id=txtExamTemplateName value=\"" + ExamTemplateCls.ExamTemplateName + "\"></td>" +
                    "          </tr>" +
                    "          <tr>" +
                    "              <td class=\"Col1\">Thời gian làm bài:</td>" +
                    "              <td><input class=textbox id=txtExamTime value=\"" + ExamTemplateCls.ExamTime + "\"></td>" +
                    "          </tr>" +
                    "          <tr>" +
                    "              <td class=\"Col1\">Số câu hỏi:</td>" +
                    "              <td><input readonly class=textbox id=txtMaxQuestion value=\"" + ExamTemplateCls.MaxQuestion + "\"></td>" +
                    "          </tr>" +

                    "          <tr>" +
                    "              <td class=\"Col1\">Điểm đạt:</td>" +
                    "              <td><input class=textbox id=txtPassPoint value=\"" + ExamTemplateCls.PassPoint + "\"></td>" +
                    "          </tr>" +
                    "          <tr>" +
                    "              <td class=\"Col1\">Thứ tự:</td>" +
                    "              <td><input class=textbox id=txtIndexOrder value=\"" + ExamTemplateCls.IndexOrder + "\"></td>" +
                    "          </tr>" +

                    "          <tr>" +
                    "              <td class=\"Col1\">Là đề thi tự luận:</td>" +
                    "              <td><input type=\"checkbox\" id=\"chkIsFreeText\" " + Checked + "></td>" +
                    "          </tr>" +

                    "           <input type=hidden id=txtExamTemplateId value=\"" + ExamTemplateId + "\">" +
                    "      </table>\r\n" +
                    " </div>\r\n" +
                    //GetConfigHtml(SiteId, ExamTemplateId) +
                     GetConfigHtmlByAccountType(SiteId, ExamTemplateId, AccountType);
                    //DrawExamCollection(SiteId).HtmlContent +
                    //"     <h3>Danh sách câu hỏi trong mẫu đề thi</h3>" +
                    //"<div id=\"divQuestionList\" class=\"tabcontent\">" +
                    //    DrawExamTemplateQuestion(SiteId, ExamTemplateId, 0).HtmlContent +
                    //"</div>\r\n";
                    
                OAjaxOut.HtmlContent = RetHtml;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.HtmlContent = ex.Message.ToString();
                OAjaxOut.InfoMessage = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut DrawExamTemplateQuestion(
            string SiteId,
            string ExamTemplateId,
            int CurrentPageIndex)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                string Html = string.Empty;
                DataTable dtQuestion = GroupQuestionBll.ReadingQuestionForGroupQuestionByExamTempId(SiteId, ExamTemplateId);
                if (dtQuestion.Rows.Count == 0)
                {
                    Html += "<div class=CssNoData>Không có câu hỏi nào thuộc nhóm câu hỏi này</div>\r\n";
                }
                else
                {
                    ReturnPaging OReturnPaging = WebPaging.GetPaging(dtQuestion.Rows.Count, CurrentPageIndex, 25, 10, "ClientRefresh");
                    Html +=
                        OReturnPaging.PagingText +
                           "<div class=tabcontent id=divListQuestionResult>\r\n" +
                           "    <table>\r\n" +
                           "        <tr>\r\n" +
                           "            <th class=thIndex>STT</th>\r\n" +
                           "            <th>Câu hỏi</th>\r\n" +
                           "            <th>Kiểu</th>\r\n" +
                           "            <th>Mức độ</th>\r\n" +
                           "            <th></th>\r\n" +
                           "            <th></th>\r\n" +
                           "        </tr>\r\n";

                    for (int iIndex = OReturnPaging.StartIndex; iIndex < OReturnPaging.EndIndex; iIndex++)
                    {
                        string QuestionId = (string)dtQuestion.Rows[iIndex]["QuestionId"];
                        string Type = (string)dtQuestion.Rows[iIndex]["Type"];
                        string Title = (string)dtQuestion.Rows[iIndex]["Title"];
                        int Level = int.Parse(dtQuestion.Rows[iIndex]["Level"].ToString());
                        string level = "Dễ";
                        if (Level == 1)
                            level = "Trung bình";
                        else if (Level == 2)
                            level = "Khó";

                        Html +=
                           "        <tr>\r\n" +
                           "            <td class=tdIndex>" + (iIndex + 1) + "</td>\r\n" +
                           "            <td><span class=CssQuestionSubject><a href=\"#\">" + Title + "</a></span></td>\r\n" +
                           "            <td>" + Type + "</td>\r\n" +
                           "            <td>" + level + "</td>\r\n" +
                            //"            <td class=tdButton><a href=\"javascript:DeleteQuestion('" + GroupQuestionId + "','" + QuestionId + "');\"><img alt=\"Xóa câu hỏi\" title=\"Xóa câu hỏi\" class=imagebutton src=\"" + IconRoot + "/delete.png\"></a></td>\r\n" +
                            //"            <td class=tdButton><a href=\"javascript:UpdateQuestion('" + GroupQuestionId + "','" + QuestionId + "');\"><img alt=\"Sửa câu hỏi\" title=\"Sửa\" class=imagebutton src=\""+IconRoot+"/Update.png\"></a></td>\r\n" +
                            //"            <td class=tdButton><a href=\"javascript:ViewQuestion('" + GroupQuestionId + "','" + QuestionId + "');\"><img alt=\"Xem câu hỏi\" title=\"Xem câu hỏi\" class=imagebutton src=\""+IconRoot+"/preview.png\"></a></td>\r\n" +
                            //"            <td class=tdButton><a href=\"AddDataToGroupQuestion.aspx?GroupQuestionId=" + GroupQuestionId + "&QuestionId=" + QuestionId + "\"><img alt=\"Phân vào nhóm\" title=\"Phân vào nhóm\" class=imagebutton src=\"Resources/Images/Category.png\"></a></td>\r\n" +
                           "           <td style=\"width:100px;\"><div id=\"divQuestion" + QuestionId + "\"><input type=button class=button onclick=\"javascript:ClientRemoveExamQuestionTemp('" + QuestionId + "','" + ExamTemplateId + "');\" value=\"Bỏ chọn\"></div></td>" +
                           "        </tr>\r\n";
                    }
                    Html += "</table>\r\n" +
                            "</div>\r\n";
                }
                OAjaxOut.HtmlContent = Html;

            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod]
        public static string GetSubExamCollection(string SiteId, string ExamCollectionId)
        {
            int Margin = 4;
            ExamCollectionCls[] ExamCollections = ExamCollectionBll.ReadingExamCollection(SiteId, ExamCollectionId);
            string SelectExamCollection =
                "<select id=\"drpSelectSubExamCollection\" class=drplist>\r\n";
            for (int iIndex = 0; iIndex < ExamCollections.Length; iIndex++)
            {
                SelectExamCollection += " <option value=\"" + ExamCollections[iIndex].ExamCollectionId + "\">" + ExamCollections[iIndex].ExamCollectionName + "</option>\r\n";
                SelectExamCollection += GetItemSubExamCollection(SiteId, ExamCollections[iIndex].ExamCollectionId, Margin);
            }
            SelectExamCollection += "</select>\r\n";

            return SelectExamCollection;
        }

        public static string GetItemSubExamCollection(string SiteId, string ExamCollectionId, int Margin)
        {
            ExamCollectionCls[] ExamCollections = ExamCollectionBll.ReadingExamCollection(SiteId, ExamCollectionId);
            string SelectExamCollection = "";
            for (int iIndex = 0; iIndex < ExamCollections.Length; iIndex++)
            {
                string ExamCollectionName = ExamCollections[iIndex].ExamCollectionName;
                for (int iMargin = 0; iMargin < Margin; iMargin++)
                {
                    ExamCollectionName = "-" + ExamCollectionName;
                }
                SelectExamCollection +=
                    " <option value=\"" + ExamCollections[iIndex].ExamCollectionId + "\">" + ExamCollectionName + "</option>\r\n";
                Margin = Margin + 4;
                SelectExamCollection += GetItemSubExamCollection(SiteId, ExamCollections[iIndex].ExamCollectionId, Margin);
                Margin = Margin - 4;
            }


            return SelectExamCollection;
        }

        private static string GetConfigHtml(string SiteId, string ExamTemplateId)
        {
            //doc bo su tap
            ExamCollectionCls[] ExamCollections = ExamCollectionBll.ReadingExamCollection(SiteId, null);
            string FirstExamCollectionId = "";
            string SelectExamCollection =
                "<select id=drpSelectExamCollection class=drplist onchange=\"javascript:ClientDetectSubExamCollection();\">\r\n";
            for (int iIndex = 0; iIndex < ExamCollections.Length; iIndex++)
            {
                SelectExamCollection += " <option value=\"" + ExamCollections[iIndex].ExamCollectionId + "\">" + ExamCollections[iIndex].ExamCollectionName + "</option>\r\n";
                if (iIndex == 0)
                {
                    FirstExamCollectionId = ExamCollections[iIndex].ExamCollectionId;
                }
            }
            SelectExamCollection += "</select>\r\n";

            string[] Types = new string[]{
                    "BestAnswer","TrueFalse","MultiChoice","CrossLink","Image","FreeText"
                };
            string[] SubjectTypes = new string[]{
                    "Câu hỏi đúng nhất","Câu hỏi cả Đúng và sai","Câu hỏi nhiều đáp án đúng","Câu hỏi ghép hợp","Câu hỏi hình ảnh","Câu hỏi luận"
                };


            string SelectTypeText = "<select class=cssComboboxContent id=\"drpSelectType\">";
            for (int iIndex = 0; iIndex < Types.Length; iIndex++)
            {
                SelectTypeText += "<option value=\"" + Types[iIndex] + "\">" + SubjectTypes[iIndex] + " (" + Types[iIndex] + ")</option>";
            }
            SelectTypeText += "</select>";



            string SelectLevelText =
                            "<select id=drpSelectLevelQuestion>\r\n" +
                            "   <option value=\"0\">Mức độ dễ</option>\r\n" +
                            "   <option value=\"1\">Mức độ trung bình</option>\r\n" +
                            "   <option value=\"2\">Mức độ khó</option>\r\n" +
                            "</select>\r\n";

            string SelectSubExamCollection = GetSubExamCollection(SiteId, FirstExamCollectionId);
            string Html = "<h3>Thiết lập cấu hình</h3>" +
                          "<div class=CssFormSaveUpdate>\r\n" +
                          "<table>" +
                          "     <tr>" +
                          "         <td class=\"Col1\">Từ bộ câu hỏi:</td>" +
                          "         <td>" + SelectExamCollection + "</td>" +
                          "     </tr>" +
                          "     <tr>" +
                          "         <td class=\"Col1\">Chọn chuyên mục:</td>" +
                          "         <td><div id=divSubExamCollection>" + SelectSubExamCollection + "</div></td>" +
                          "     </tr>" +
                          "     <tr>" +
                          "         <td class=\"Col1\">Loại câu hỏi:</td>" +
                          "         <td>" + SelectTypeText + "</td>" +
                          "     </tr>" +
                          "          <tr>" +
                          "              <td class=\"Col1\">Độ khó dễ:</td>" +
                          "              <td>" + SelectLevelText + "</td>" +
                          "          </tr>" +
                          "     <tr>" +
                          "         <td class=\"Col1\">Số câu hỏi:</td>" +
                          "         <td><input id=txtQuestion value=\"10\"></td>" +
                          "     </tr>" +

                          "     <tr>" +
                          "         <td></td>" +
                          "         <td><input onclick=javascript:Add2Template('" + ExamTemplateId + "'); type=button  class=button  value=\"Đưa vào mẫu\"></td>" +
                          "     </tr>" +
                          "</table>" +
                          "</div>\r\n" +
                          "<div id=divConfigItem class=tabcontent>" + GetConfigListHtml(SiteId, ExamTemplateId) + "</div>";
            //string Html = DrawExamCollection(SiteId).HtmlContent;
            return Html;
        }


        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut DrawGroupQuestion(
            string SiteId,
            string RootGroupQuestionId,
            string AccountType)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                string Html = string.Empty;
                GroupQuestionCls[] OGroupQuestion = GroupQuestionBll.ReadingGroupQuestion(SiteId, RootGroupQuestionId, AccountType: AccountType);
                if (OGroupQuestion.Length == 0)
                {
                    Html = "Bạn không được phân quyền vào nhóm câu hỏi nào!";
                    OAjaxOut.HtmlContent = Html;
                    return OAjaxOut;
                }
                Html =
                              "<script language=javascript>\r\n" +
                              "     function ClientLoadQuestion(GroupQuestionId)\r\n" +
                              "     {\r\n" +
                              "         SiteId='" + SiteId + "';\r\n" +
                              "         document.getElementById('txtSelectedGroupQuestionId').value = GroupQuestionId;\r\n" +
                              "     }\r\n" +

                              "     function ClientRefresh(CurrentPageIndex)\r\n" +
                              "     {\r\n" +
                              "         SiteId='" + SiteId + "';\r\n" +
                              "         GroupQuestionId=document.getElementById('txtSelectedGroupQuestionId').value;\r\n" +
                              "         document.getElementById('txtCurrentPageIndex').value=CurrentPageIndex;\r\n" +
                              "         ClientLoadQuestion(GroupQuestionId);\r\n" +
                              "     }\r\n" +
                              "</script>\r\n" +
                              "<input type=hidden id=\"txtCurrentPageIndex\" value=\"0\">\r\n" +
                              "<input type=hidden id=\"txtSelectedGroupQuestionId\" value=\"" + RootGroupQuestionId + "\">\r\n" +
                              "<h3>Ngân hàng câu hỏi</h3>\r\n" +
                              "<div class=TreeHolder>\r\n" +
                    "<div id=\"tree\">\r\n" +
                    "<ul>\r\n";

                for (int iIndex = 0; iIndex < OGroupQuestion.Length; iIndex++)
                {
                    bool CheckFunAcces = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).CheckPermission(SiteId, GroupQuestionPermission.StaticId, GroupQuestionPermission.funcAccess, OGroupQuestion[iIndex].GroupQuestionId, WebSessionUtility.GetCurrentLoginUser().UserId, false);
                    if (!CheckFunAcces)
                        continue;

                    Html +=
                    " <li class=\"folder expanded\"><a href=\"" + OGroupQuestion[iIndex].GroupQuestionId + "\" target=\"_self\">" + OGroupQuestion[iIndex].GroupQuestionName + "</a>\r\n" +
                    "     <ul>\r\n";
                    GroupQuestionCls[] GroupQuestions = GroupQuestionBll.ReadingGroupQuestion(SiteId, OGroupQuestion[iIndex].GroupQuestionId);
                    for (int iIndex1 = 0; iIndex1 < GroupQuestions.Length; iIndex1++)
                    {
                        Html +=
                                "<li class=\"folder expanded\"><a href=\"" + GroupQuestions[iIndex1].GroupQuestionId + "\" target=\"_self\">" + GroupQuestions[iIndex1].GroupQuestionName + "</a>\r\n" +
                                    BuildSubTreeView(SiteId, GroupQuestions[iIndex1].GroupQuestionId).HtmlContent +
                                "</li>\r\n";
                    }

                    Html += "</ul>\r\n" +
                            "</li>\r\n";
                }
                Html += "</ul>\r\n" +
                            "</div>\r\n" +
                            "</div>\r\n";
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut BuildSubTreeView(
            string SiteId,
            string ParentGroupQuestionId
            )
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                StringBuilder Html = new StringBuilder();
                GroupQuestionCls[] GroupQuestions = GroupQuestionBll.ReadingGroupQuestion(SiteId, ParentGroupQuestionId);

                Html.Append("<ul>\r\n");
                for (int iIndex = 0; iIndex < GroupQuestions.Length; iIndex++)
                {
                    Html.Append(
                            "<li class=\"folder expanded\"><a target=\"_self\" href=\"" + GroupQuestions[iIndex].GroupQuestionId + "\">" + GroupQuestions[iIndex].GroupQuestionName + "</a>\r\n" +
                                BuildSubTreeView(SiteId, GroupQuestions[iIndex].GroupQuestionId).HtmlContent +
                            "</li>\r\n");
                }

                Html.Append("</ul>\r\n");
                OAjaxOut.HtmlContent = Html.ToString();
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        private static string GetConfigHtmlByAccountType(
            string SiteId,
            string ExamTemplateId,
            string AccountType            
            )
        {            
            string GroupQuestionTreeView = DrawGroupQuestion(SiteId, "", AccountType).HtmlContent;          

            string[] Types = new string[]{
                    "BestAnswer","TrueFalse","MultiChoice","CrossLink","Image","FreeText"
                };
            string[] SubjectTypes = new string[]{
                    "Câu hỏi đúng nhất","Câu hỏi cả Đúng và sai","Câu hỏi nhiều đáp án đúng","Câu hỏi ghép hợp","Câu hỏi hình ảnh","Câu hỏi luận"
                };

            string SelectTypeText = "<select class=cssComboboxContent id=\"drpSelectType\">";
            for (int iIndex = 0; iIndex < Types.Length; iIndex++)
            {
                SelectTypeText += "<option value=\"" + Types[iIndex] + "\">" + SubjectTypes[iIndex] + " (" + Types[iIndex] + ")</option>";
            }
            SelectTypeText += "</select>";



            string SelectLevelText =
                            "<select id=drpSelectLevelQuestion>\r\n" +
                            "   <option value=\"0\">Mức độ dễ</option>\r\n" +
                            "   <option value=\"1\">Mức độ trung bình</option>\r\n" +
                            "   <option value=\"2\">Mức độ khó</option>\r\n" +
                            "</select>\r\n";
            
            string Html = "<h3>Thiết lập cấu hình</h3>" +
                          "<div class=CssFormSaveUpdate>\r\n" +
                          "<table>" +

                          "     <tr>" +
                          "         <td class=\"Col1\">Từ bộ câu hỏi:</td>" +
                          "         <td>" + GroupQuestionTreeView + "</td>" +
                         "     </tr>" +

                          "     <tr>" +
                          "         <td class=\"Col1\">Loại câu hỏi:</td>" +
                          "         <td>" + SelectTypeText + "</td>" +
                          "     </tr>" +
                          "          <tr>" +
                          "              <td class=\"Col1\">Độ khó dễ:</td>" +
                          "              <td>" + SelectLevelText + "</td>" +
                          "          </tr>" +
                          "     <tr>" +
                          "         <td class=\"Col1\">Số câu hỏi:</td>" +
                          "         <td><input id=txtQuestion value=\"10\"></td>" +
                          "     </tr>" +

                          "     <tr>" +
                          "         <td></td>" +
                          "         <td><input onclick=javascript:Add2TemplateByAccountType('" + ExamTemplateId + "'); type=button  class=button  value=\"Đưa vào mẫu\"></td>" +
                          "     </tr>" +
                          "</table>" +
                          "</div>\r\n" +
                          "<div id=divConfigItem class=tabcontent>" + GetConfigListHtmlNew(SiteId, ExamTemplateId) + "</div>";            
            return Html;
        }

        public static string GetPluginJavascript(string SiteId)
        {
            string Html =
             "<script language=javascript>\r\n" +
                //     string SiteId,
                //string ExamTemplateId,
                //int CurrentPageIndex)
                "     function ClientLoadQuestion()\r\n" +
                                  "     {\r\n" +
                                  "         SiteId='" + SiteId + "';\r\n" +
                                  "         CurrentPageIndex=parseInt(document.getElementById('txtCurrentPageIndex').value);\r\n" +
                                  "         ExamTemplateId=document.getElementById('txtExamTemplateId').value;\r\n" +
                                  "         AjaxOut = Portal.Exam.Render.UpdateExamTemplate.DrawExamTemplateQuestion(SiteId,ExamTemplateId,CurrentPageIndex).value;\r\n" +
                                  "         document.getElementById('divQuestionList').innerHTML=AjaxOut.HtmlContent;\r\n" +
                                  "     }\r\n" +

                                  "     function ClientRefresh(CurrentPageIndex)\r\n" +
                                  "     {\r\n" +
                                  "         SiteId='" + SiteId + "';\r\n" +
                                  "         document.getElementById('txtCurrentPageIndex').value=CurrentPageIndex;\r\n" +
                                  "         ClientLoadQuestion();\r\n" +
                                  "     }\r\n" +

                " function ClientRemoveExamQuestionTemp(QuestionId,ExamTemplateId)\r\n" +
                        " {\r\n" +
                        "     SiteId='" + SiteId + "';\r\n" +
                        "     CurrentPageIndex = document.getElementById('txtCurrentPageIndex').value;\r\n" +
                        "     AjaxOut=Portal.Exam.Render.UpdateExamTemplate.ServerSideRemoveTemplate(SiteId,ExamTemplateId,QuestionId,CurrentPageIndex).value;\r\n" +
                        "     if(AjaxOut.Error)\r\n" +
                        "     {\r\n" +
                        "         alert(AjaxOut.InfoMessage);\r\n" +
                        "         return;\r\n" +
                        "     }\r\n" +
                        "      document.getElementById('divQuestion'+QuestionId).innerHTML='';\r\n" +
                        " }\r\n" +

                " function JumPageQuestion(PageIndex) \r\n" +
                " { \r\n" +
                "     document.getElementById('txtPageIndex').value=PageIndex;\r\n" +
                "     document.getElementById(\"divProcessing\").innerHTML='Đang tìm kiếm...'; \r\n" +
                "     setTimeout(\"RealJumpPageQuestion()\",10); \r\n" +
                " } \r\n" +

                " function RealJumpPageQuestion() \r\n" +
                " { \r\n" +
                "     SiteId='" + SiteId + "';\r\n" +
                "     PageIndex=document.getElementById(\"txtPageIndex\").value; \r\n" +
                "     GroupQuestionId=document.getElementById(\"drpSelectGroupQuestion\").options[document.getElementById(\"drpSelectGroupQuestion\").selectedIndex].value; \r\n" +
                "     SubGroupQuestionName=document.getElementById(\"txtSubGroupQuestionName\").value; \r\n" +
                "     Type=document.getElementById(\"drpSelectType\").options[document.getElementById(\"drpSelectType\").selectedIndex].value; \r\n" +
                "     Keyword=document.getElementById(\"txtKeyword\").value; \r\n" +
                "     SelectLevelIndex = document.getElementById(\"drpSelectLevelQuestion\").selectedIndex; \r\n" +
                "     SelectLevel = document.getElementById(\"drpSelectLevelQuestion\").options[SelectLevelIndex].value; \r\n" +

                //"     ExamCollectionId=document.getElementById('txtSelectedExamCollectionId').value;\r\n" +
                "     ExamTemplateId=document.getElementById('txtExamTemplateId').value;\r\n" +
                "     retValue=Portal.Exam.Render.UpdateExamTemplate.ServerSideSearch(SiteId, GroupQuestionId,SubGroupQuestionName,Type, Keyword,SelectLevel, ExamTemplateId, PageIndex).value; \r\n" +
                "     if(retValue.indexOf(\"ERROR\")!=-1) \r\n" +
                "     { \r\n" +
                "         alert(retValue); \r\n" +
                "         return; \r\n" +
                "     } \r\n" +
                "     document.getElementById(\"divProcessing\").innerHTML=\"\"; \r\n" +
                "     document.getElementById(\"divResultContent\").innerHTML=retValue; \r\n" +
                " } \r\n" +


                " function ClientSelectExamQuestion(QuestionId)\r\n" +
                " {\r\n" +
                "     ExamTemplateId=document.getElementById('txtExamTemplateId').value;\r\n" +
                "    SelectLevelIndex = document.getElementById(\"drpSelectLevelQuestion\").selectedIndex; \r\n" +
                "    SelectLevel = document.getElementById(\"drpSelectLevelQuestion\").options[SelectLevelIndex].value; \r\n" +
                "     AjaxOut=Portal.Exam.Render.UpdateExamTemplate.ServerSideAddToTemplate(SiteId,ExamTemplateId,  QuestionId,SelectLevel).value;\r\n" +
                "     if(AjaxOut.Error)\r\n" +
                "     {\r\n" +
                "         alert(AjaxOut.InfoMessage);\r\n" +
                "         return;\r\n" +
                "     }\r\n" +
                "      document.getElementById('divQuestion'+QuestionId).innerHTML='';\r\n" +
                " }\r\n" +


                " function ResetSearch()\r\n" +
                "  {\r\n" +
                "     document.getElementById(\"divProcessing\").innerHTML=\"\";\r\n" +
                "     document.getElementById(\"divResultContent\").innerHTML=\"\";\r\n" +
                "     document.getElementById(\"divResultInfo\").innerHTML=\"???\";\r\n" +
                " }\r\n" +


                "</script>\r\n" +
                "<input type=hidden id=\"txtCurrentPageIndex\" value=\"0\">\r\n";

            return Html;
        }

        [AjaxPro.AjaxMethod]
        public AjaxOut ServerSideRemoveTemplate(
            string SiteId,
            string ExamTemplateId,
            string QuestionId,
            string CurrentPageIndex
            )
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                new QuestionBll().RemoveQuestionFromExamTemp(SiteId, ExamTemplateId, QuestionId);
                OAjaxOut.HtmlContent = DrawExamTemplateQuestion(SiteId, ExamTemplateId, int.Parse(CurrentPageIndex)).HtmlContent;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }


        [AjaxPro.AjaxMethod]
        public static string ServerSideSearch(
            string SiteId,
            string GroupQuestionId,
            string SubGroupQuestionName,
            string Type,
            string Keyword,
            string SelectLevel,
            string ExamTemplateId,
            int CurrentPageIndex)
        {
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                DataTable dtSearchExamQuestion = new QuestionBll().SearchQuestionWithTemplateId(SiteId, GroupQuestionId, SubGroupQuestionName, Type, SelectLevel, ExamTemplateId, Keyword);
                ReturnPaging OReturnPaging = WebPaging.GetPaging(dtSearchExamQuestion.Rows.Count, CurrentPageIndex, 50, 10, "JumPageQuestion");
                string RetHtml =
                    OReturnPaging.PagingText +
                    "   <table style=\"width:100%\">" +
                    "       <tr>" +
                    "           <th>#</th>" +
                    "           <th>Câu hỏi</th>" +
                    "           <th>Nhóm</th>" +
                    "           <th style=\"text-align:right\">Điểm</th>" +
                    "           <th></th>" +
                    "       </tr>";

                for (int iIndex = OReturnPaging.StartIndex; iIndex < OReturnPaging.EndIndex; iIndex++)
                {
                    string QuestionId = (string)dtSearchExamQuestion.Rows[iIndex]["QuestionId"];
                    string QuestionName = (string)dtSearchExamQuestion.Rows[iIndex]["Title"];
                    string RootGroupQuestionName = (string)dtSearchExamQuestion.Rows[iIndex]["RootGroupQuestionName"];
                    string GroupQuestionName = (string)dtSearchExamQuestion.Rows[iIndex]["GroupQuestionName"];
                    string DisplayText = GroupQuestionName;
                    if (!RootGroupQuestionName.Equals(GroupQuestionName))
                    {
                        DisplayText = RootGroupQuestionName + " / " + GroupQuestionName;
                    }
                    int MaxPoint = (int)dtSearchExamQuestion.Rows[iIndex]["MaxPoint"];
                    RetHtml +=
                    "       <tr onMouseOver=\"this.className='highlight'\" onMouseOut=\"this.className='normal'\">" +
                    "           <td class=tdIndex>" + (iIndex + 1) + "</td>" +
                    "           <td>" + QuestionName + "</td>" +
                    "           <td>" + DisplayText + "</td>" +
                    "           <td class=tdIndex>" + MaxPoint.ToString("#,##0") + "</td>" +
                    "           <td style=\"width:100px;\"><div id=\"divQuestion" + QuestionId + "\"><input type=button class=button onclick=\"javascript:ClientSelectExamQuestion('" + QuestionId + "');\" value=\"Chọn câu hỏi này\"></div></td>" +
                    "       </tr>";
                }
                RetHtml +=
                    "   </table>\r\n" +
                    "</div>";
                return RetHtml;
            }
            catch (Exception ex)
            {
                return ex.Message.ToString();
            }
        }

        public static AjaxOut DrawExamCollection(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                string IconRoot = WebConfig.GetButtonIconRoot(OSiteParam);
                string WebPartLayoutRoot = WebConfig.GetWebPartLayoutIconRoot(OSiteParam);

                GroupQuestionCls[] GroupQuestions = GroupQuestionBll.ReadingGroupQuestion(SiteId, "");
                string SelectGroupQuestionText = "<select class=drplist id=drpSelectGroupQuestion onchange=javascript:DetectMonthi();>";
                for (int iIndex = 0; iIndex < GroupQuestions.Length; iIndex++)
                {
                    string GroupQuestionId = GroupQuestions[iIndex].GroupQuestionId;
                    string Name = GroupQuestions[iIndex].GroupQuestionName;
                    SelectGroupQuestionText += "<option value=\"" + GroupQuestionId + "\">" + Name + "</option>";
                }
                SelectGroupQuestionText += "</select>";


                string[] Types = new string[]{
                    "BestAnswer","TrueFalse","MultiChoice","CrossLink","Image","FreeText"
                };
                string[] SubjectTypes = new string[]{
                    "Câu hỏi 1 phương án đúng nhất","Câu hỏi dạng đúng hoặc sai","Câu hỏi hơn 1 phương án đúng","Câu hỏi liên kết","Câu hỏi hinh ảnh","Câu hỏi luận",
                };

                string SelectTypeText = "<select class=drplist id=drpSelectType onchange=javascript:JumPageQuestion(0);>";
                for (int iIndex = 0; iIndex < Types.Length; iIndex++)
                {
                    SelectTypeText += "<option value=\"" + Types[iIndex] + "\">" + SubjectTypes[iIndex] + " (" + Types[iIndex] + ")</option>";
                }
                SelectTypeText += "</select>";


                string SelectLevelText =
                    "<select id=drpSelectLevelQuestion class=drplist>\r\n" +
                    "   <option value=\"0\">Mức độ dễ</option>\r\n" +
                    "   <option value=\"1\">Mức độ trung bình</option>\r\n" +
                    "   <option value=\"2\">Mức độ khó</option>\r\n" +
                    "</select>\r\n";


                string RetHtml =
                        " <div class=DatabaseFunction>\r\n" +
                        "        <ul>\r\n" +
                        "            <li onclick=\"javascript:JumPageQuestion(0);\"><img class=image src=\"" + IconRoot + "/Search.png\"><div>Tìm kiếm</div></li>\r\n" +
                        "            <li onclick=\"javascript:JumPageGroupQuestion(0);\"><img class=image src=\"" + IconRoot + "/Refresh.png\"><div>Đọc lại</div></li>\r\n" +
                        "        </ul>\r\n" +
                        "  </div>\r\n" +
                        " <input id=txtPageIndex value=\"0\" type=hidden>\r\n" +
                        " <h3 style=\"text-align:left;\">Xác định câu hỏi cho mẫu đề thi</h3>\r\n" +
                        " <div class=CssFormSaveUpdate>\r\n" +
                        "   <table>" +

                        "       <tr>" +
                        "           <td class=Col1>Nhóm câu hỏi:</td>" +
                        "           <td class=Col2>" + SelectGroupQuestionText + "</td>" +
                        "       </tr>" +

                        "       <tr>" +
                        "           <td class=Col1>Kiểu câu hỏi:</td>" +
                        "           <td class=Col2>" + SelectTypeText + "</td>" +
                        "       </tr>" +
                        "       <tr>" +
                        "           <td class=Col1>Mức độ:</td>" +
                        "           <td class=Col2>" + SelectLevelText + "</td>" +
                        "       </tr>" +

                        "       <tr>" +
                        "           <td class=Col1>Phân loại:</td>" +
                        "           <td class=Col2><input id=txtSubGroupQuestionName type=textbox class=textbox></div></td>" +
                        "       </tr>" +

                        "       <tr>" +
                        "           <td class=Col1>Tiêu đề câu hỏi: </td>" +
                        "           <td class=Col2><input class=textbox type=textbox id=txtKeyword></td>" +
                        "       </tr>" +
                        "   </table>" +
                        " </div>\r\n" +
                        "<div id=divProcessing></div>" +
                        "<div id=\"divResultContent\" class=\"tabcontent\">" +
                        "       <div class=CssNoData>Nhập các tiêu chí tìm kiếm và tiến hành tìm kiếm câu hỏi</div>" +
                        "</div>\r\n";



                OAjaxOut.HtmlContent = RetHtml;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }


        [AjaxPro.AjaxMethod]
        public static string GetConfigListHtml(string SiteId, string ExamTemplateId)
        {
            DataTable dtItems = new ExamTemplateBll().ReadingItems(SiteId, ExamTemplateId);
            string Html =
                  "<table>" +
                         "     <tr>" +
                         "         <th class=cssTdItemConfig1>#</th>" +
                         "         <th class=cssTdItemConfig2>Loại câu hỏi</th>" +
                         "         <th class=cssTdItemConfig3>Loại</th>" +
                         "         <th class=cssTdItemConfig4>Số câu</th>" +
                         "         <th class=cssTdItemConfig5></th>" +
                         "         <th class=cssTdItemConfig6></th>" +
                         "     </tr>";
            for (int iIndex = 0; iIndex < dtItems.Rows.Count; iIndex++)
            {
                string ExamTemplateConfigId = (string)dtItems.Rows[iIndex]["ExamTemplateConfigId"];
                int LevelQuestion = 0;
                try
                {
                    LevelQuestion = (int)dtItems.Rows[iIndex]["LevelQuestion"];
                }
                catch { }
                string LevelQuestionText = "Câu hỏi dễ";
                if (LevelQuestion == 1)
                {
                    LevelQuestionText = "Câu hỏi trung bình";
                }
                if (LevelQuestion == 2)
                {
                    LevelQuestionText = "Câu hỏi khó";
                }
                int TQuestion = (int)dtItems.Rows[iIndex]["NoOfQuestion"];
                string RootExamCollectionName = (string)dtItems.Rows[iIndex]["RootExamCollectionName"];
                string ExamCollectionName = (string)dtItems.Rows[iIndex]["ExamCollectionName"];
                Html +=
                         "     <tr>" +
                         "         <td style=\"width:30px;\">" + (iIndex + 1) + "</td>" +
                         "         <td >" + RootExamCollectionName + "<br>" + ExamCollectionName + "<br>" + LevelQuestionText + "</td>" +
                         "         <td >" + (string)dtItems.Rows[iIndex]["QuestionType"] + " (" + TQuestion.ToString("#,##0") + ")</td>" +
                         "         <td ><input class=cssTxtQuestion value=\"" + (int)dtItems.Rows[iIndex]["NoOfQuestion"] + "\" id=\"txtQuestion_" + ExamTemplateConfigId + "\"></td>" +
                         "         <td style=\"width:30px;\"><input type=button class=button value='Xóa' onclick=\"javascript:DeleteItemConfig('" + ExamTemplateId + "','" + ExamTemplateConfigId + "');\"></td>" +
                         "         <td style=\"width:30px;\"><input type=button class=button value='Ghi lại' onclick=\"javascript:SaveItemConfig('" + ExamTemplateConfigId + "');\"></td>" +
                         "     </tr>";
            }

            Html +=
                          "</table>";
            return Html;

        }

        [AjaxPro.AjaxMethod]
        public static string GetConfigListHtmlNew(string SiteId, string ExamTemplateId)
        {
            DataTable dtItems = new ExamTemplateBll().ReadingItemsConfig(SiteId, ExamTemplateId);
            string Html =
                  "<table>" +
                         "     <tr>" +
                         "         <th class=cssTdItemConfig1>#</th>" +
                         "         <th class=cssTdItemConfig2>Nhóm câu hỏi</th>" +
                         "         <th class=cssTdItemConfig3>Loại câu hỏi</th>" +
                         "         <th class=cssTdItemConfig3>Mức độ</th>" +
                         "         <th class=cssTdItemConfig4>Số câu</th>" +
                         "         <th class=cssTdItemConfig5></th>" +
                         "         <th class=cssTdItemConfig6></th>" +
                         "     </tr>";
            for (int iIndex = 0; iIndex < dtItems.Rows.Count; iIndex++)
            {
                string ExamTemplateConfigId = (string)dtItems.Rows[iIndex]["ExamTemplateConfigId"];
                int LevelQuestion = 0;
                try
                {
                    LevelQuestion = (int)dtItems.Rows[iIndex]["LevelQuestion"];
                }
                catch { }
                string LevelQuestionText = "Câu hỏi dễ";
                if (LevelQuestion == 1)
                {
                    LevelQuestionText = "Câu hỏi trung bình";
                }
                if (LevelQuestion == 2)
                {
                    LevelQuestionText = "Câu hỏi khó";
                }
                int TQuestion = (int)dtItems.Rows[iIndex]["TQuestion"];
                int NoOfQuestion = (int)dtItems.Rows[iIndex]["NoOfQuestion"];
                string QuestionType = (string)dtItems.Rows[iIndex]["QuestionType"];
                
                string RootExamCollectionName = (string)dtItems.Rows[iIndex]["RootGroupQuestionName"];
                string ExamCollectionName = (string)dtItems.Rows[iIndex]["GroupQuestionName"];
                Html +=
                         "     <tr>" +
                         "         <td style=\"width:30px;\">" + (iIndex + 1) + "</td>" +
                         "         <td >" + RootExamCollectionName + "<br>" + ExamCollectionName + "<br></td>" +
                         "         <td >" + QuestionType + " (" + TQuestion.ToString("#,##0") + ")</td>" +
                         "         <td >" + LevelQuestionText + "</td>" +                         
                         "         <td ><input class=cssTxtQuestion value=\"" + NoOfQuestion + "\" id=\"txtQuestion_" + ExamTemplateConfigId + "\"></td>" +
                         "         <td style=\"width:30px;\"><input type=button class=button value='Xóa' onclick=\"javascript:DeleteItemConfig('" + ExamTemplateId + "','" + ExamTemplateConfigId + "');\"></td>" +
                         "         <td style=\"width:30px;\"><input type=button class=button value='Ghi lại' onclick=\"javascript:SaveItemConfig('" + ExamTemplateConfigId + "');\"></td>" +
                         "     </tr>";
            }

            Html +=
                          "</table>";
            return Html;

        }

        [AjaxPro.AjaxMethod]
        public string ServerSideUpdateExamTemplate(
            string SiteId,
            string ExamTemplateId,
            string ExamTemplateName,
            string ExamTime,
            string MaxQuestion,
            string PassPoint,
            string IndexOrder,
            string BeforeExamGuidle,
            int ActiveExam,
            bool IsFreeText)
        {
            try
            {
                WebSessionUtility.CheckSessionTimeOut();

                if (ExamTemplateName.Trim().Length == 0) throw new Exception("Nhập tên nhóm vai trò");
                if (FunctionUtilities.checkInteger(ExamTime) == false)
                {
                    throw new Exception("Thời gian nhập sai!");
                }
                if (FunctionUtilities.checkInteger(PassPoint) == false)
                {
                    throw new Exception("Điểm đạt nhập sai!");
                }
                if (FunctionUtilities.checkInteger(IndexOrder) == false)
                {
                    throw new Exception("Thứ tự nhập sai!");
                }
                new ExamTemplateBll().UpdateExamTemplate(null, SiteId, ExamTemplateId, ExamTemplateName, int.Parse(ExamTime), int.Parse(MaxQuestion), int.Parse(PassPoint), int.Parse(IndexOrder), BeforeExamGuidle, ActiveExam, IsFreeText);
                return "Cập nhật thành công!";
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }


        [AjaxPro.AjaxMethod]
        public string ServerSideSaveItemConfig(
            string SiteId,
            string ExamTemplateConfigId,
            int NoOfQuestion)
        {
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                new ExamTemplateBll().SaveExamTemplateConfig(null, SiteId, ExamTemplateConfigId, NoOfQuestion);
                return "Cập nhật thành công!";
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }



        [AjaxPro.AjaxMethod]
        public string ServerSideCheckDeleteExamTemplateConfig(string SiteId, string ExamTemplateConfigId)
        {
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                return "";
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string ServerSideDeleteExamTemplateConfig(string SiteId, string ExamTemplateConfigId)
        {
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                new ExamTemplateBll().DeleteExamTemplateConfig(null, SiteId, ExamTemplateConfigId);
                return "Xóa thành công";
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }


        [AjaxPro.AjaxMethod]
        public string GetConfirmDeleteMsg(string SiteId)
        {
            return "Bạn đã chắc chắn xóa chưa?";
        }


        [AjaxPro.AjaxMethod]
        public string ServerSideAddExamTemplateConfigNew(
            string SiteId,
            string ExamTemplateId,
            string GroupQuestionId,
            string QuestionType,
            int NoOfQuestion,
            string SelectLevel)
        {
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                new ExamTemplateBll().AddExamTemplateConfigNew(null, SiteId, ExamTemplateId, QuestionType, GroupQuestionId, NoOfQuestion, int.Parse(SelectLevel));
                return "Đưa vào template thành công!";
            }
            catch (Exception ex)
            {
                string ErrorMsg = ex.Message.ToString();
                if (ErrorMsg.IndexOf("duplicate key") != -1)
                {
                    ErrorMsg = "Đã đưa nhóm câu hỏi với độ khó này vào trong danh sách rồi!";
                }
                return "ERROR: " + ErrorMsg;
            }
        }


        [AjaxPro.AjaxMethod]
        public string ServerSideAddExamTemplateConfig(
            string SiteId,
            string ExamTemplateId,
            string ExamCollectionId,
            string QuestionType,
            int NoOfQuestion,
            string SelectLevel)
        {
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                new ExamTemplateBll().AddExamTemplateConfig(null, SiteId, ExamTemplateId, QuestionType, ExamCollectionId, NoOfQuestion, int.Parse(SelectLevel));
                return "Đưa vào template thành công!";
            }
            catch (Exception ex)
            {
                string ErrorMsg = ex.Message.ToString();
                if (ErrorMsg.IndexOf("duplicate key") != -1)
                {
                    ErrorMsg = "Đã đưa nhóm câu hỏi với độ khó này vào trong danh sách rồi!";
                }
                return "ERROR: " + ErrorMsg;
            }
        }


        //[AjaxPro.AjaxMethod]
        //public string ReadingMonthi(string SiteId,string GroupQuestionId)
        //{
        //    DataTable dtMonthi = new MonthiBll().ReadingMonthi(SiteId, GroupQuestionId, "");
        //    string SelectMonthiText = "<select class=cssComboboxContent id=drpSelectMonthi onchange=javascript:ResetSearch();>";
        //    for (int iIndex = 0; iIndex < dtMonthi.Rows.Count; iIndex++)
        //    {
        //        string MonthiId = (string)dtMonthi.Rows[iIndex]["MonthiId"];
        //        string Name = (string)dtMonthi.Rows[iIndex]["Name"];
        //        int Question = (int)dtMonthi.Rows[iIndex]["Question"];
        //        SelectMonthiText += "<option value=\"" + MonthiId + "\">" + Name + " ("+Question+")</option>";
        //    }
        //    SelectMonthiText += "</select>";
        //    return SelectMonthiText;
        //}
    }
}